//
//  CLStack.m
//  Charles
//
//  Created by Leo on 2019/6/27.
//  Copyright © 2019 charles. All rights reserved.
//

#import "CLStack.h"

@interface CLArrayStack () {

}

@property (nonatomic, assign) NSInteger capacity;   // 栈容量
@property (nonatomic, assign) NSInteger elementCnt; // 当前栈中的元素个数
@property (nonatomic, assign) NSInteger *items;

@end

@implementation CLArrayStack

- (instancetype)initWithCapacity:(NSInteger)capacity {
    if (self = [super init]) {
        self.capacity = capacity;
        self.elementCnt = 0;
        [self setupStackCapacity];
    }
    return self;
}

- (void)setupStackCapacity {
    self.items = malloc(sizeof(NSInteger) * self.capacity);
}

- (BOOL)push:(NSInteger)obj {
    if (self.elementCnt == self.capacity) {
        NSLog(@"没有容量了...");
        return NO;
    }
    self.items[self.elementCnt] = obj;
    NSLog(@"入栈成功了...");
    self.elementCnt++;
    return YES;
}

- (NSInteger)pop {
    if (self.elementCnt == 0) {
        NSLog(@"没有元素了...");
        return 0;
    }
    NSInteger tmp = self.items[self.elementCnt - 1];
    NSLog(@"出栈成功...");
    self.elementCnt--;
    return tmp;
}
@end
